﻿using MahApps.Metro.Controls;
using PLCWpfTest.Common;
using PLCWpfTest.Common.Extensions;
using PLCWpfTest.Models;
using PLCWpfTest.ViewModels;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using static System.Collections.Generic.Dictionary<long, string>;

namespace PLCWpfTest.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        private DoubleAnimation _doubleAnimation;

        private Storyboard _storyboard;

        private DispatcherTimer _timer;

        public MainWindow(MainViewModel mainViewModel)
        {
            InitializeComponent();
            DataContext = mainViewModel;
            _doubleAnimation = new DoubleAnimation();
            _doubleAnimation.From = 0;
            Storyboard.SetTargetName(_doubleAnimation, nameof(leftShiftTransform));
            Storyboard.SetTargetProperty(_doubleAnimation, new PropertyPath(TranslateTransform.XProperty));
            _storyboard = new Storyboard
            {
                RepeatBehavior = RepeatBehavior.Forever
            };
            _storyboard.Children.Add(_doubleAnimation);
            _timer = new DispatcherTimer(TimeSpan.FromMilliseconds(1000), DispatcherPriority.Normal, (s, e) =>
            {
                if (_doubleAnimation.To != -leftShiftList.DesiredSize.Width)
                {
                    _storyboard.Stop(leftShiftPanel);
                    _doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(((ValueCollection)leftShiftList.ItemsSource).Count * 3));
                    _doubleAnimation.To = -leftShiftList.DesiredSize.Width;
                    _storyboard.Begin(leftShiftPanel, true);
                }
                DataCache.UpdateLongDateModelPairs();
            }, Dispatcher);
            _timer.Start();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Escape && WindowState == WindowState.Maximized)
            {
                MaximizeWindow();
            }
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.OnMouseDownMethod(e);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            MessageBoxResult mbr = this.ShowQuestion("确定要退出系统吗");
            if (mbr != MessageBoxResult.OK)
            {
                e.Cancel = true;
                return;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            _timer.Stop();
            CommunicationCache.Uninitialize();
        }

        private void MinBtn_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void MaxBtn_Click(object sender, RoutedEventArgs e)
        {
            MaximizeWindow();
        }

        private void MaximizeWindow()
        {
            ControlModel controlModel = (ControlModel)FindResource("MaxBtnModel");
            if (WindowState == WindowState.Maximized)
            {
                controlModel.Content = "\ue7b2";
                controlModel.Visibility = Visibility.Visible;
                WindowState = WindowState.Normal;
            }
            else
            {
                controlModel.Content = "\ue7b1";
                controlModel.Visibility = Visibility.Collapsed;
                WindowState = WindowState.Maximized;
            }
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}